var app = angular.module("manApp",["ngRoute"]);
    app.config(function($routeProvider) {
	$routeProvider.when("/list", {
		controller: listController,
		templateUrl: "list.html"
	}).when("/view/:id", {
		controller: homeController,  
		templateUrl: "home.html"
	}).otherwise({
		redirectTo: "/list"
	})
}) 
   
   function listController($scope, $http,$rootScope) {
   	$scope.sex="男";
   	//默认初始当前第几页值为1；
   	$scope.page=1;
   	//首页 尾页 上一页 下一页  事件绑定
   	$scope.replace=function(page){
		if(page<1){
			page=1;return;
		}
		if(page>$scope.total){
			page=$scope.total;
			return;
		}
		$scope.query({page:page,age:$scope.order});
		$scope.page=page;
	} 
	
	
	//查询所有学生信息	
	$scope.query=function(param){
		$http({
			method: 'GET',
			url: 'list',
			params:param
		}).then(function(response) {
			$rootScope.list = response.data.data;//获取数组
			$scope.total=Math.ceil(response.data.total);//获取总页数
			$scope.count=response.data.count;//获取总条数
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});
	}
	
	
	
	
  	//批量删除
   	$scope.delAll=function(){
   		//获取选中的input value值   （value="{{user._id}}input中value=id值）；
		var boxList=angular.element("input[type=checkbox]:checked");
		if(confirm("确定要删除以上"+boxList.length+"条信息吗？"))
		{
			//遍历id
			boxList.each(function(index,ele){
				//执行删除
				$scope.cvll(ele.value);
			})
		}
	}
	//删除一个学生
	$scope.del = function(index) {
		if(!confirm("确定要删除吗")) return;
		$scope.cvll(index);
	}
	//按年龄排序
//	$scope.orderby=true;
//	$scope.order=1;
//	$scope.didi=function(){
//		$scope.orderby=!$scope.orderby;
//		$scope.order=$scope.orderby?1:-1;
//		$scope.query({page:$scope.page,age:$scope.order});
//	}

	$scope.order=1;
	$scope.didi=function(){
		if( $scope.order==1){
			$scope.order=-1;
		}else{
			$scope.order=1;
		}
		$scope.query({page:$scope.page,age:$scope.order});
	}
	
	
	
	
   $scope.query({page:$scope.page,age:$scope.order});
   //删除学生信息
   $scope.cvll=function(index){
    	$http({
			method: 'POST',
			url: 'del',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}, //设置headers
			data: "id=" + index
		}).then(function(response) {
			if(response.data == "success") {
				location.reload();
			} else {
				alert("系统繁忙，请稍后再试");
			}
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});
    }
  	
	
   //添加一个学生信息
	$scope.sbb = function(){
		var obj={
			name:$scope.name,
			age:$scope.age,
			sex:$scope.sex
		}
		if($scope.id || $scope.id==0){
			obj.id=$scope.id;
		}
		$http({
			method: 'POST',
			url: 'save',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}, //设置headers
			data: $.param(obj)
		}).then(function(response) {
			if(response.data == "success") {
				location.reload();
			} else {
				alert("系统繁忙，请稍后再试");
			}
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});
	}


	//修改学生信息
	$scope.sbbb=function(id,index){
		var user=$rootScope.list[index];
		$scope.name=user.username;
		$scope.age=parseInt(user.userage);
		$scope.sex=user.usersex;
		$scope.id=id;
	}


//模糊搜索
$scope.sou=function(){
		var name=$scope.username;
		$http({
			method: 'POST',
			url: 'sou',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}, //设置headers
			data: "name=" + name
		}).then(function(response) {
			$rootScope.list = response.data;
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});	
	}

//全选or全不选
	$scope.checks=false;
	$scope.checkAll=function(){
	 	$scope.checks=!$scope.checks;
  }
	
	
}
function homeController($scope,$routeParams,$rootScope) {
	var id = $routeParams["id"];
	$scope.user=$rootScope.list[id];
}






